import zhLang from '../language/zh.js'
import enLang from '../language/en.js'
import {
	createStore
} from 'vuex'
// 1. 是否有本地缓存
// 语言标识
let langFlag = uni.getStorageSync("langFlag");
// 当前语言包
let lang
// 2. 没有用户设置，取系统语言
if (!langFlag) {
	const sys = uni.getSystemInfoSync();
	langFlag = sys.language;
}
// 根据语言读取文件
if (langFlag.substring(0, 2) == 'zh') {
	lang = zhLang
	changeTabbarText(lang)
} else {
	lang = enLang
	changeTabbarText(lang)
}


// 根据语言包改变tabbar的text
function changeTabbarText(lang){
	lang.tabbarTextList.forEach((item,index)=>{
		uni.setTabBarItem({
			index,
			text: item.text
		})
	})
}

const store = createStore({
	state: { //存放状态
		lang
	},
	mutations: {
		changeLang(state) {
			uni.showActionSheet({
				itemList: ['简体中文', 'English'],
				success(e) {
					if (e.tapIndex == 0) {
						lang = zhLang
					} else {
						lang = enLang
					}
					uni.setStorageSync("langFlag", lang.langType)
					state.lang = lang
					changeTabbarText(lang)
				}
			})
		}
	}
})

export default store
